#include "my_lib/pwm.h"


void PwmInit(void)
{
    ATIM_InitTypeDef ATIM_InitStruct;
    ATIM_OCInitTypeDef ATIM_OCInitStruct;
    GPIO_InitTypeDef GPIO_InitStruct;
    //开时钟
    __RCC_ATIM_CLK_ENABLE();
    __RCC_GPIOB_CLK_ENABLE();    //引脚时钟

    //引脚
    GPIO_InitStruct.IT = GPIO_IT_NONE;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pins = GPIO_PIN_5;  //引脚号
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    PB05_AFx_ATIMCH1A();     //引脚复用
    PB04_AFx_ATIMCH3B();                     
    GPIO_Init(CW_GPIOB, &GPIO_InitStruct);  
    GPIO_InitStruct.Pins = GPIO_PIN_4;  //引脚号
    GPIO_Init(CW_GPIOB, &GPIO_InitStruct);  

    //tim
    ATIM_InitStruct.BufferState = DISABLE;
    ATIM_InitStruct.ClockSelect = ATIM_CLOCK_PCLK;
    ATIM_InitStruct.CounterAlignedMode = ATIM_COUNT_MODE_EDGE_ALIGN;
    ATIM_InitStruct.CounterDirection = ATIM_COUNTING_UP;
    ATIM_InitStruct.CounterOPMode = ATIM_OP_MODE_REPETITIVE;
    ATIM_InitStruct.OverFlowMask = DISABLE;
    ATIM_InitStruct.Prescaler = ATIM_Prescaler_DIV64; // 计数时钟1MHz
    ATIM_InitStruct.ReloadValue = 500;              // 2kHz, 0.5ms
    ATIM_InitStruct.RepetitionCounter = 0;
    ATIM_InitStruct.UnderFlowMask = DISABLE;
    ATIM_Init(&ATIM_InitStruct);

    ATIM_OCInitStruct.BufferState = ENABLE;
    ATIM_OCInitStruct.OCDMAState = DISABLE;
    ATIM_OCInitStruct.OCInterruptSelect = ATIM_OC_IT_UP_COUNTER;
    ATIM_OCInitStruct.OCInterruptState = ENABLE;
    ATIM_OCInitStruct.OCMode = ATIM_OCMODE_PWM1;
    ATIM_OCInitStruct.OCPolarity = ATIM_OCPOLARITY_NONINVERT;
    ATIM_OC1AInit(&ATIM_OCInitStruct);  //通道配置
    ATIM_OC3BInit(&ATIM_OCInitStruct);

    ATIM_CH1Config(ATIM_CHxA_CIE, ENABLE);
    ATIM_CH3Config(ATIM_CHxB_CIE, ENABLE);
    ATIM_SetCompare1A(0);   //初始化为0
    ATIM_SetCompare3B(0);
    ATIM_Cmd(ENABLE);
    ATIM_CtrlPWMOutputs(ENABLE);
}


